package com.trip.gateway.filter;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;

import com.trip.gateway.util.RedisUtil;
import com.trip.gateway.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

//@Component
public class AuthFilter implements GlobalFilter, Ordered {
    @Autowired
    private RedisUtil redisUtil;

    private static String[] whitelist  = new String[]{
            "/trip-user/user/loginByAccount",
            "/trip-user/user/loginByEmail",
            "/trip-user/user/sendEmail",
            "/trip-user/user/register",
            "/trip-user/user/checkIsRegisterByUserName",
            "/trip-user/user/checkIsRegisterByEmail",
            "/trip-user/user/checkIsRegisterByPhone",
            "/trip-driver/driver/loginByAccount",
            "/trip-driver/driver/loginByEmail",
            "/trip-driver/driver/sendEmail"
    };

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String uri = String.valueOf(request.getURI().getPath());
        for (String whiteUrl:whitelist
        ) {
            if (uri.contains(whiteUrl)){
                return chain.filter(exchange);
            }
        }

        String token = request.getHeaders().getFirst("Authorization");
        System.out.println(token);
        if (StringUtils.isBlank(token)) {

            return buildReturnMono("没有认证",exchange);
        }else {
            if (("null").equals(redisUtil.get(token))){
                return buildReturnMono("认证已过期",exchange);
            }else {
                if (redisUtil.get(token).equals(token)){
                    return buildReturnMono("认证无效",exchange);
                }
            }
        }

        return chain.filter(exchange);
    }


    private Mono<Void> buildReturnMono(String error, ServerWebExchange exchange) {
        ServerHttpResponse response = exchange.getResponse();
        Map<String,Object> map = new HashMap<>();
        map.put("code",403);
        map.put("message",error);

        String jsonString = JSON.toJSONString(map);
        byte[] bits = jsonString.getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = response.bufferFactory().wrap(bits);
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        return response.writeWith(Mono.just(buffer));
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
